<html>
<head>
	<TITLE></TITLE>
	<META content="text/html; charset=utf-8" http-equiv=Content-Type>
	<script src="../js/jquery-3.1.1.min.js">


	</script>
	<script src="../js/top.js">


	</script>
	<script src="../js/jquery.cookie-1.4.1.min.js">


	</script>
</head>
<body>

<div id="top">

</div>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
	<tr>
		<td valign="top" width="5%" align="left">
			<table align="left" bgcolor="#008800" border="0" cellspacing="2"
				cellpadding="2">
				<tr>
					<td bgcolor="#FFFF88">
						<a href="main.html"><b><font color="BLACK" size="2">&lt;&lt; 主菜单</font>
				</b></a>
					</td>
				</tr>
			</table>
		</td>
		<td valign="top" align="center">
			<h2 align="center">
				
			</h2>

				
				<table align="center" bgcolor="#008800" border="0" cellspacing="2"
					cellpadding="5" id="list" >
					<tr bgcolor="#cccccc" id="0">
						<td>
                                项目编号
                            </td>
                            <td>
                                产品编号
                            </td>
                            <td>
                                产品描述
                            </td>
                            <td>
                                <b>项目描述</b>
                            </td>
                            <td width="100">
                                单价
                            </td>
                            <td>
                                数量
                            </td>
                            <td>
                                合计
                            </td>
                            <td>
                                操作
                            </td>
					</tr>

			
			
					
					

					
					
			

				</table>
				<center>


				</center>



			<br />
			<center>
				<input type="image" src="../images/button_checkout.gif"  id="checkout"/>

				</a>
			</center>


		</td>




	</tr>
</table>
<script type="text/javascript">
	
	$("#checkout").click(function () {

        var ORDERS_CHECKOUT="http://localhost:8080/cart/checkout";
        var username=$.cookie("pusername");

        if(username==undefined){
            window.location="login.html";
            return;
        }
        var orders={
            orderid:$("#orderid").val(),
            username:username,
            orderdate:new Date(),
            totalprice:$("#sum").html()
        };
        $.ajax({
            url:ORDERS_CHECKOUT,
			type:"POST",
			data:JSON.stringify(orders),
			contentType:"application/json",
			statusCode:{
                200:function(){
                    window.location="main.html?username="+username;
				}
			}
		});
    });
	
    function show(){
        var t=location.href;
        var i=t.lastIndexOf("?");
        var path=t.substring(i+1,t.length);
        var oid=path.split("=")[1];//订单编号

        var username=$.cookie("pusername");

        if(username==undefined){
            window.location="login.html";
            return;
        }
        var CART_QUERY_URL="http://localhost:8080/cart/query/";

        $.ajax({
            url:CART_QUERY_URL+oid+"/"+username,
            type:"GET",
            dataType:"json",
            statusCode:{
                200:function(data){
                    $("#list tr:not(:first)").remove();
                    var sum=0;
                    var orderis=0;
                    $(data).each(function(index){
                        var i=$("#list>tbody>tr:last").attr("id");

                        i++;
                        orderid=data[index].orderid;
                        sum+=data[index].item.listprice*data[index].quantity;
                        var str="<tr bgcolor='#FFFF88' id='"+i+"'>"+
                            "<td><a href='item.html?itemid="+data[index].item.itemid+"'>"+data[index].item.itemid+"</a></td>"+
                            "<td>"+data[index].item.product.productid+"</td>"+
                            "<td>"+data[index].item.product.descn+"</td>"+
                            "<td>"+data[index].item.attr1+"</td>"+
                            "<td>"+data[index].item.listprice+"</td>"+
                            "<td><input type='number' value='"+data[index].quantity+"'  onblur='update(this,\""+data[index].item.itemid+"\","+data[index].orderid+")'/></td>"+
                            "<td>"+data[index].item.listprice*data[index].quantity+"</td>"+
                            "<td>" +
							"<input type='image' src='../images/button_remove.gif' onclick='del(\""+data[index].item.itemid+"\","+data[index].orderid+")'>"+
							"</td>"+
                            "</tr>";
                        $("#list").append(str);
                    });
                        var str1="<tr bgcolor='#FF8'><td colspan='5'></td>" +
							"<td>总计：</td>" +
							"<td><input type='hidden' id='orderid' value='"+orderid+"'/><span id='sum'>"+sum+"</span></td>" +
							"</tr>";

                        $("#list").append(str1);

                }
            }


        });

    }

    function update(obj,itemid,oid){
        var username=$.cookie("pusername");
        if(username==undefined){
            window.location="login.html";
            return;
        }
        var CART_UPDATE_URL="http://localhost:8080/cart/up";


        var json={
            itemid:itemid,
            username:username,
            orderid:oid,
			quantity:obj.value

        };
        $.ajax({
            url:CART_UPDATE_URL,
            data:JSON.stringify(json),
            contentType:"application/json",
            type:"PUT",
            dataType:"text",//String
            statusCode:{
                200:function(data){
                    show();
                }
            }


        });


    }
    function del(itemid,oid){

        var username=$.cookie("pusername");
        if(username==undefined){
            window.location="login.html";
            return;
        }
        var CART_DEL_URL="http://localhost:8080/cart/del";


        var json={
            itemid:itemid,
            username:username,
            orderid:oid

        };
        $.ajax({
            url:CART_DEL_URL,
            data:JSON.stringify(json),
            contentType:"application/json",
            type:"DELETE",
            dataType:"text",//String
            statusCode:{
                200:function(data){
                    show();
                }
            }


        });


    }
	show();
    </script>
<br>
</body>
</html>